/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.darly.std;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.darly.chinese.base.BaseActivity;
import com.darly.chinese.controller.fileload.ExternalStorageUtil;
import com.darly.chinese.db.chinese.bean.SongCiModel;
import com.darly.chinese.event.BaseEvent;
import com.darly.chinese.event.EventController;
import com.darly.chinese.table.BlackTable;
import com.darly.dlcommon.app.ActionView;
import com.darly.dlcommon.app.App;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlndk.controller.NDKController;
import com.darly.imageeditor.editimage.EditImageActivity;
import com.darly.rnmodule.ModuleEnum;
import com.darly.rnmodule.ui.MainPageActivity;
import com.darly.rnmodule.ui.MapPageActivity;
import com.darly.rnmodule.ui.ObjectListActivity;
import com.darly.rnmodule.ui.PayEntityActivity;
import com.darly.rnmodule.ui.ReactNativeMainActivity;
import com.darly.rnmodule.ui.SettingPageActivity;
import com.darly.rnmodule.ui.ShopStoreActivity;
import com.darly.rnmodule.ui.VisitActivity;
import com.darly.std.crm.SongCiCreateActivity;
import com.darly.std.databinding.ActivityMainBinding;
import com.darly.std.ui.BlackTableActivity;
import com.darly.std.ui.CollectionActivity;
import com.darly.std.ui.ListViewActivity;
import com.darly.std.ui.RecyclerViewActivity;
import com.darly.std.vm.ActionWrapper;
import com.darly.std.vm.MainViewModel;
import com.darly.std.widget.view.DoMainView;
import com.darly.widget.titlebar.TitleBar;

import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

/**
 * Description 该类功能为：首界面
 * Package com.darly.std
 *
 * @author zhangyuhui
 * @date 2019/8/21
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> implements TitleBar.OnRightButtonClickListener {

    @Override
    protected TitleBar getTitleBar() {
//        binding.toolbar.setCenterTitle(R.string.app_name);
//        setSupportActionBar(binding.toolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
//        binding.toolbar.setRightButtonText("测试");
//        binding.toolbar.setOnRightButtonClickListener(this);
//        binding.toolbar.setLeftVisiable(false);
//        return binding.toolbar;
        return null;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected int initVariableId() {
        return BR.mainViewModel;
    }

    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        OperController.init();
//        binding.toolbar.post(new Runnable() {
//            @Override
//            public void run() {
//                if (!SpController.getInstance().getBoolean(SpEnum.FIRST_OPEN)) {
//                    viewModel.firstGuideView(binding.toolbar);
//                }
//            }
//        });
        DoMainView view = binding.idMainDomain;
        view.connect();
    }


    @Override
    public void initObservableView() {
        viewModel.getAction().observe(this, new Observer<ActionView>() {
            @Override
            public void onChanged(ActionView action) {
                if (action != null) {
                    doAction(action);
                }
            }
        });
    }


    /**
     * 开始调用
     *
     * @param action 传递Action
     */
    private void doAction(ActionView action) {
        if (action.getModule() == ActionWrapper.TIME_COUNT || action.getModule() == ActionWrapper.IMAGE_EDIT) {
            //这两个不需要intent跳转。
            switch (action.getModule()) {
                case ActionWrapper.TIME_COUNT:
                    int count = action.getParam(Integer.class);
                    binding.idProgress.setProgress(count);
                    break;
                case ActionWrapper.IMAGE_EDIT:
                    EditImageActivity.start(this, ExternalStorageUtil.getDownLoadPath() + File.separator + "pic.jpg", 200);
                    break;
            }
        } else {
            Intent intent = new Intent();
            switch (action.getModule()) {
                case App.NEXT_PAGE:
                    String authorModel = action.getParam(String.class);
                    intent.setClass(this, RecyclerViewActivity.class);
                    intent.putExtra("Title", authorModel);
                    break;
                case ActionWrapper.TABLE_EDIT:
                    NDKController.getInstance().init();
                    intent.setClass(this, BlackTableActivity.class);
                    intent.putExtra("Title", "表格");
                    startActivityForResult(intent, 100);
                    return;
                case ActionWrapper.LIST_VIEW_CLICK:
                    intent.setClass(this, ListViewActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, "ListViewActivity");
                    break;
                case ActionWrapper.COLLECTION_CLICK:
                    intent.setClass(this, CollectionActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, "CollectionActivity");
                    break;
                case ActionWrapper.RN_MAIN_VIEW:
                    intent.setClass(this, ReactNativeMainActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.REACT_NATIVE_MAIN_ACTIVITY);
                    break;
                case ActionWrapper.MAIN_PAGE:
                    intent.setClass(this, MainPageActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.MAIN_PAGE_ACTIVITY);
                    break;
                case ActionWrapper.MAP_PAGE:
                    intent.setClass(this, MapPageActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.MAP_PAGE_ACTIVITY);
                    break;
                case ActionWrapper.SETTING_PAGE:
                    intent.setClass(this, SettingPageActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.SETTING_PAGE_ACTIVITY);
                    break;
                case ActionWrapper.OBJECT_LIST:
                    intent.setClass(this, ObjectListActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.OBJECT_LIST_ACTIVITY);
                    break;
                case ActionWrapper.PAY_ENTITY:
                    intent.setClass(this, PayEntityActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.PAY_ENTITY_ACTIVITY);
                    break;
                case ActionWrapper.VISIT_ENTITY:
                    intent.setClass(this, VisitActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.VISIT_ACTIVITY);
                    break;
                case ActionWrapper.DETAIL_ENTITY:
                    intent.setClass(this, ShopStoreActivity.class);
                    intent.putExtra(ModuleEnum.TITLE, ModuleEnum.SHOP_STORE_ACTIVITY);
                    break;
            }
            startActivity(intent);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        EventController.register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventController.unregister(this);
    }


    @Override
    public void onUiMessage(BaseEvent event) {
        super.onUiMessage(event);
        DLog.d("MainActivity", "onUiMessage() called with: event = [" + event.position() + "]");
    }

    @Override
    public void onTrMessage(BaseEvent event) {
        super.onTrMessage(event);
        DLog.d("MainActivity", "onTrMessage() called with: event = [" + event.position() + "]");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
//        // Get the SearchView and set the searchable configuration
//        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//        SearchView searchView = (SearchView) menu.findItem(R.id.id_menu_search).getActionView();
//        searchView.setQueryHint("搜索内容");
//        // Assumes current activity is the searchable activity
//        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                return false;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String newText) {
//                return false;
//            }
//        });
//        //searchview 的关闭监听
//        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
//            @Override
//            public boolean onClose() {
//                return false;
//            }
//        });
//        //根据id-search_src_text获取TextView
//        SearchView.SearchAutoComplete searchViewOfKnowledge = searchView.findViewById(R.id.search_src_text);
//        //改变输入文字的颜色
//        searchViewOfKnowledge.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
//        if (id == R.id.id_menu_search) {
//            return true;
//        }
        switch (id) {
            case R.id.id_menu_setting:
                //设置界面
            case R.id.id_menu_help:
                //帮助界面
                Toast.makeText(this, "功能未实现", Toast.LENGTH_LONG).show();
                break;
            case R.id.id_menu_collection:
                //统计界面
                Intent intent = new Intent(this, CollectionActivity.class);
                intent.putExtra("Title", "统计");
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == 100) {
            ArrayList<BlackTable> tables = data.getParcelableArrayListExtra("KEY");
        }

        if (requestCode == 200) {
            viewModel.updateImage(ExternalStorageUtil.getDownLoadPath() + File.separator + "pic.jpg");
        }
    }

    @Override
    public void onClick(View view) {
        //测试新建功能
        if (new Random().nextBoolean()) {
            SongCiCreateActivity.goCreate(this);
        } else {
            SongCiModel data = new SongCiModel(UUID.randomUUID(), "key", "KKKKK", "hans");
            SongCiCreateActivity.goEdit(this, "编辑", data);
        }

        //统计界面
//        Intent intent = new Intent(this, CollectionActivity.class);
//        intent.putExtra("Title", "统计");
//        startActivity(intent);
    }
}
